#include "AEEAppGen.h"
#include "AEERootForm.h"
#include "dfon_comdef.h"

#include "mgallery.h"
#include "assign.h"
#include "rename.h"
#include "catform.h"
#include "catinfo.h"
#include "mediaintfmg.h"

static void OnRootFormChange(void *data, ModelEvent* event)
{
	MGallery *me = data;
	if(IROOTFORM_GetTopForm(me->rf) == NULL && !IPTLAYER_IsDisplayRotationInProgress(me->ptl))
		ISHELL_CloseApplet(me->shell, FALSE);
}

boolean MGallery_OnStart(MGallery *me, AEEAppStart *appStart)
{
	IResFile *themeFile = NULL;
	IModel *model = NULL;

	//add listener to rootform
	IFORM_GetFormModel(IROOTFORM_TO_IFORM(me->rf), &model);
	if(model)
	{
		IMODEL_AddListenerEx(model, &me->rfListener, OnRootFormChange, me);
		RELEASEIF(model);
	}

	//load numberimages array
	IROOTFORM_GetThemeFile(me->rf, &themeFile);
	if(themeFile)
	{
		IImage *image = NULL;
		int i = 0;
		for(i=0; i<10; i++)
		{
			char imageName[50];
			SNPRINTF(imageName, sizeof(imageName), "Num.nImages.kwc_numbered_menu_item%d.Image", i+1);
			IRESFILE_GetNamedImage(themeFile, (void**)&image, imageName, 0);
			if(image)
				IVECTORMODEL_InsertAt(me->numberImages, i, image);
		}
		IRESFILE_GetNamedImage(themeFile, (void**) &me->rightArrow, "FullscreenWin.Controls.kwc_right_arrow.Image",0);
		IRESFILE_GetNamedImage(themeFile, (void**) &me->leftArrow, "FullscreenWin.Controls.kwc_left_arrow.Image",0);
		RELEASEIF(themeFile);
	}

	STRCPY(me->resFileName, "kmglang_en.bar");

	//try to parse input params
	if(appStart->pszArgs){
		MG_LaunchDS *launch = (MG_LaunchDS *)ATOI(appStart->pszArgs);
		if(launch->m_inFlags & F_MG_CHOOSE){
			launch->m_outFlags = EFAILED;
			me->outValue = &launch->m_outFlags;
			me->mode = MGalleryModeChooseSound;
			me->outFileName = launch->m_LaunchData.m_sChooseMedia.fileName;
		}
	}
	
	return CatForm_Create(me, CatInfo_GetStartCatID(me->mode));
}

boolean MGallery_HandleEvent(MGallery *me , AEEEvent evt, uint16 wParam, uint32 dwParam)
{
	if(me->ptl && IPTLAYER_HandleEvent(me->ptl, evt, wParam, dwParam))
		return TRUE;

	return FALSE;
}

void MGallery_Destroy(MGallery *me)
{
	if(!me)
		return;

	LISTENER_Cancel(&me->rfListener);
	Assign_Destroy(me);
	Rename_Destroy(me);
	RELEASEIF(me->leftArrow);
	RELEASEIF(me->rightArrow);
	RELEASEIF(me->utils);
	RELEASEIF(me->numberImages);
	RELEASEIF(me->ptl);
	RELEASEIF(me->sndLib);

	FREEIF(me);
}

MGallery* MGallery_Create(IShell *shell)
{
	MGallery *me = MALLOCREC(MGallery);
	me->shell = shell;

	ISHELL_CreateInstance(me->shell, AEECLSID_DIFFON_WIDGET_LIB, (void**)&me->ptl);
	ISHELL_CreateInstance(me->shell, AEECLSID_DIFFON_SOUND_LIB, (void**)&me->sndLib);
	ISHELL_CreateInstance(me->shell, AEECLSID_DIFFON_UTILITY_LIB, (void**)&me->utils);
	ISHELL_CreateInstance(me->shell, AEECLSID_VECTORMODEL, (void**)&me->numberImages);
	if(!me->ptl || !me->sndLib || !me->numberImages || !me->utils)
	{
		MGallery_Destroy(me);
		return NULL;
	}

	IVECTORMODEL_SetPfnFree(me->numberImages, VF_RELEASE);
	IPTLAYER_SetClassID(me->ptl, me, 0);
	IPTLAYER_GetRootForm(me->ptl, &me->rf);
	return me;
}